<?php 

class Event_CallCenter2PostFetch extends Crud_Event_Handler 
{
	protected $_type = 'postFetch';
	
	/**
	 * Run handler with options array.
	 * 
	 * @return void
	 */
	public function run($rowset)
	{
		$orderTypeRowset = OrderTypePeer::getInstance()->fetchAll();
		$orderTypeRowset = OrderTypePeer::getInstance()->normalizeByPreferKey($orderTypeRowset);
		
		$operatorRowset = OrderOperatorPeer::getInstance()->fetchAll();
		$operatorRowset = OrderOperatorPeer::getInstance()->normalizeByPreferKey($operatorRowset);
		
		$statusRowset = OrderStatusPeer::getInstance()->fetchAll();
		$statusRowset = OrderOperatorPeer::getInstance()->normalizeByPreferKey($statusRowset);
		
		$paymentRowset = PaymentMethodsPeer::getInstance()->fetchAll();
		$paymentRowset = PaymentMethodsPeer::getInstance()->normalizeByPreferKey($paymentRowset);
		
		$orderTypeCallback = OrderTypePeer::getInstance()->fetchByKey('callback');
		
		$sections = array(
			'orders' =>  AdminSectionPeer::getInstance()->fetchByKey('orders'),
			'callback' => AdminSectionPeer::getInstance()->fetchByKey('callback')
		);
		$ids = array(
			'orders' => array(),
			'callback' => array()
		);
		
		foreach($rowset as &$row) {
			
			if ($orderTypeCallback->getId() == $row['type_id']) {
				 $ids['callback'][] = $row['id'];
			} else {
				$ids['orders'][] = $row['id'];
			}
			
		}
		
		
		if (count($ids['callback']) > 0) {
			$this->lockRowset($rowset, $ids['callback'], $sections['callback'], $orderTypeCallback->getId());
		}
		if (count($ids['orders']) > 0) {
			$this->lockRowset($rowset, $ids['orders'], $sections['orders'], $orderTypeCallback->getId());
		}
		
		return $rowset;
	}
	
	
	function lockRowset(&$rowset, $ids, $sectionRow, $callbackTypeId) {
			
		$select = AdminLockPeer::getInstance()->select();
		$select->where(AdminLockPeer::SECTION_ID, $sectionRow->getId());
		$select->where(AdminLockPeer::ROW_ID . Db_Select::SQL_IN, $ids);
		$ago = date('Y-m-d H:i:s', strtotime('-15 min'));
		$select->where(AdminLockPeer::CREATED . Db_Select::SQL_GREATER_EQUAL, $ago);
		
		
		$lockedRowset = AdminLockPeer::getInstance()->fetchAll($select);
		$lockedRowset = AdminLockPeer::getInstance()->normalizeByPreferKey($lockedRowset, 'row_id');
		
		foreach ($rowset as &$row) {
			if (isset($lockedRowset[$row['id']])
			 && ($sectionRow->getKey() == 'callback' || $callbackTypeId != $row['type_id'])
			) {
				$lockedRow = $lockedRowset[$row['id']];
				$lUser = $lockedRow->getAdminUserRowByAdminUserId();
				$adminUser = $this->getAdminUser();
				if ($lUser->getLogin() != $adminUser->getLogin()) {
					$lUserName = $lUser->getName() ? $lUser->getName() : $lUser->getLogin(); 
					$row['crud_locked'] = $lUserName;
				}
			}
		}
		
		unset($row);
	}
	
	public function getAdminUser() {
		Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session('crud_auth'));
		$login = Zend_Auth::getInstance()->getIdentity();
		$user = AdminUserPeer::getInstance()->findByLogin($login)->current();
		
		return $user;
	}
	
}
